package com.cloudpivot.receivales.equipmentinstallation.service.impl;

import com.cloudpivot.receivales.equipmentinstallation.domain.InstallationReportEntity;
import com.cloudpivot.receivales.equipmentinstallation.service.EquipmentInstallationReportService;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.*;
import com.deepoove.poi.util.PoitlIOUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class EquipmentInstallationReportImpl implements EquipmentInstallationReportService {

    @Value("${workorder.modepath}")
    private String MODEPATH;

    /**
     * 模版生成签字文档 by base64 输出流
     * @param entity
     */
    @Override
    public Map<String, Object> createStreambase64(InstallationReportEntity entity) {
        Map<String, Object> rspMap = new HashMap<>();
        try {
            //处理子表
            TableRenderData table=buildTable(entity);
            if (!Objects.isNull(table)) {
                entity.setTable(table);
            }
            //模版处理
            XWPFTemplate template = XWPFTemplate.compile(MODEPATH + "EquipmentInstallation.docx");
            template.render(entity);

            //字符流
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            template.write(out);
            byte[] bytes = out.toByteArray();
            out.close();
            PoitlIOUtils.closeQuietlyMulti(template, out);
            rspMap.put("bytes", bytes);
            return rspMap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 创建子表对象
     *
     * @return
     */
    private TableRenderData buildTable(InstallationReportEntity entity) {
        try {
            List<InstallationReportEntity.Item> items = entity.getItems();
            //做表头
            RowRenderData rowTitle = Rows.of("序号", "产品名称", "规格型号", "数量","序列号").center().textBold().create();

            if (Objects.isNull(items)) {
                RowRenderData[] args = new RowRenderData[1];
                args[0] = rowTitle;
                //只返回表头||返回空
                TableRenderData table = Tables.of(args).create();
                return table;
            } else {
                //返回表
                RowRenderData[] args = new RowRenderData[items.size() + 1];
                args[0] = rowTitle;
                for (int i = 0; i < items.size(); i++) {
                    InstallationReportEntity.Item item = items.get(i);
                    //列的上下一定要对上
                    item.setNum(String.valueOf(i+1));
                    args[i + 1] = Rows.of(item.getNum(),item.getProductName(), item.getModel(), item.getQuantity(), item.getSnNum()).center().create();
                }
                TableRenderData table = Tables.of(args).create();
                return table;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
